<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>账单信息</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/static/admin/style.css}" media="all">
</head>
<style>
    .layui-table[lay-even] tr:nth-child(even) {
        /* background-color: #aaffaa; */
        background-color: #eeffee;
    }
    /* 鼠标指向表格时,奇数行背景颜色 */
    .layui-table tbody tr:hover,.layui-table-hover {
        background-color: #eeffee;
    }
    /* 表格头部工具栏背景色 */
    .layui-table-tool {
        background-color: #eeffee;
    }
    /* 表格头部背景色 */
    th {
        background-color: #359f6a;
        /* MediumSeaGreen */
        color: #fff;
        font-weight: bold
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px">
            <form class="layui-form layui-form-pane" action="">
                <input name="id" th:value="${obj?.id}" class="layui-input layui-hide"/>
                <div class="layui-form-item">
                    <label class="layui-form-label">供应商</label>
                    <div class="layui-input-block">
                        <select  name="sname" id="sname"></select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">药品名称</label>
                    <div class="layui-input-block">
                        <select  name="dname" id="dname" lay-filter="drugName"></select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">药品编码</label>
                    <div class="layui-input-block">
                        <select name="dnumber" id="dnumber" ></select>
                    </div>
                </div>



                <div class="layui-form-item">
                    <label class="layui-form-label">数量</label>
                    <div class="layui-input-block">
                        <input type="text" name="count" th:value="${obj?.count}" lay-verify="required" placeholder="请输入数量" autocomplete="off" class="layui-input">
                    </div>
                </div>
<!--                <div class="layui-form-item">-->
<!--                    <label class="layui-form-label">总金额</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" name="total" th:value="${obj?.total}" lay-verify="required" placeholder="请输入总金额" autocomplete="off" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->



                <div class="layui-form-item">
                    <label class="layui-form-label">进货时间</label>
                    <div class="layui-input-block">
                        <input type="text" readonly name="buytime" id="buytime" th:value="${obj?.buytimeStr}" lay-verify="required" placeholder="请输入进货时间" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-hide">
                    <input type="button" lay-submit lay-filter="LAY-front-submit" id="LAY-front-submit" value="确认">
                </div>
            </form>
        </div>
    </div>
</div>

<script th:src="@{/static/layui/layui.js}"></script>

<script>
    layui.use(['form','laydate'],function () {
        var $ = layui.$;
        var laydate = layui.laydate;
        var form = layui.form;
        //支持日期控件
        laydate.render({
            elem: '#buytime',
            trigger: 'click'
        })


        //当前供应商的值
        var currentSname = '[[${obj?.sname}]]';
        //获取供应商列表
        $.ajax({
            url:'/supplier/supplierList',
            dataType:'json',
            type:'post',
            cache:false,
            success:function (data) {
                $('#sname').prepend("<option value=''>请选择</option>");
                for(var i=0;i<data.data.length;i++){
                    if(data.data[i].name==currentSname){
                        $('#sname').append("<option selected value='"+data.data[i].name+"'>"+data.data[i].name+"</option>")
                    }else {
                        $('#sname').append("<option value='"+data.data[i].name+"'>"+data.data[i].name+"</option>")
                    }
                }
                form.render(); //渲染下拉框
            }
        })

        //当前药品的值
        var currentDname = '[[${obj?.dname}]]';
        //当前编号
        var currentDnumber = '[[${obj?.dnumber}]]';

        //获取药品列表
        $.ajax({
            url:'/druginfo/druginfoList',
            dataType:'json',
            type:'post',
            cache:false,
            success:function (data) {
                $('#dname').prepend("<option value=''>请选择</option>");
                for(var i=0;i<data.data.length;i++){
                    if(data.data[i].name==currentDname){
                        $('#dname').append("<option selected value='"+data.data[i].name+"'>"+data.data[i].name+"</option>")
                    }else {
                        $('#dname').append("<option value='"+data.data[i].name+"'>"+data.data[i].name+"</option>")
                    }
                }
                form.render(); //渲染下拉框
                //药品编号
                selectNumber();
            }
        })
        //根据药品名称找编号
        function selectNumber() {
            $("#dnumber").empty();
            var drugName = $('#dname').val();
            console.log(drugName)
            $.ajax({
                url: '/druginfo/drugInfoByCondition?drugName=' + drugName,
                dataType: 'json',
                type: 'get',
                cache: false,
                success: function (data) {
                    if (data.data.length != null ) {
                        $('#dnumber').prepend("<option value=''>请选择</option>");
                        for (var i = 0; i < data.data.length; i++) {
                            if (data.data[i].number == currentDnumber) {
                                $('#dnumber').append("<option selected value='" + data.data[i].number + "'>" + data.data[i].number + "</option>")
                            } else {
                                $('#dnumber').append("<option value='" + data.data[i].number + "'>" + data.data[i].number + "</option>")
                            }
                        }
                        form.render(); //渲染下拉框
                    }
                }
            })
        };
        //过滤器  添加的时候 选择药品名称后会触发 药品编号
        form.on('select(drugName)', function () {
            $("#dnumber").empty();
            var drugName = $('#dname').val();
            $.ajax({
                url: '/druginfo/drugInfoByCondition?drugName=' + drugName,
                dataType: 'json',
                type: 'get',
                cache: false,
                success: function (data) {
                    $('#dnumber').prepend("<option value=''>请选择</option>");
                    for (var i = 0; i < data.data.length; i++) {
                        if (data.data[i].number == currentDnumber) {
                            $('#dnumber').append("<option selected value='" + data.data[i].number + "'>" + data.data[i].number + "</option>")
                        } else {
                            $('#dnumber').append("<option value='" + data.data[i].number + "'>" + data.data[i].number + "</option>")
                        }
                    }
                    form.render(); //渲染下拉框
                }
            })
        });
    })
</script>
</body>
</html>